import VariableDict as CM




def Method1(sp_tup):
    '''
        Moment: (3,0,0..)
    '''
    assert sum(sp_tup) == 3
    for i in sp_tup:
        assert i in (0,3)
        
    sp_list = list(sp_tup)
    idx = sp_list.index(3)
    
    numer = len(sp_list)*[0]
    numer[idx] = 2
    value = CM.Variables().getMoment(tuple(numer))
    
    
    denom = len(sp_list)*[0]
    denom[idx] = 1 
    value = value/CM.Variables().getMoment(tuple(denom))
    
    return value**3

def Method2(sp_tup):
    '''
        Moment: (2,1,0, ...)
    '''
    assert sum(sp_tup) == 3
    for i in sp_tup:
        assert i in (0,1,2)
        
    sp_list = list(sp_tup)
    idx1 = sp_list.index(1)
    idx2 = sp_list.index(2)    
    
    numer = len(sp_list)*[0]
    numer[idx2] = 2
    value = CM.Variables().getMoment(tuple(numer))
    
    denom = len(sp_list)*[0]
    denom[idx1] = 1 
    value = value/CM.Variables().getMoment(tuple(denom))
    

    numer = len(sp_list)*[0]
    numer[idx2] = 1
    numer[idx1] = 1    
    value *= CM.Variables().getMoment(tuple(numer))**2
    
    
    denom = len(sp_list)*[0]
    denom[idx2] = 1 
    value = value/CM.Variables().getMoment(tuple(denom))**2
    
    return value

def Method3(sp_tup):
    '''
        Moment: (1,1,1, ...)
    '''
    assert sum(sp_tup) == 3
    for i in sp_tup:
        assert i in (0,1)
        
    sp_list = list(sp_tup)
    idx1 = sp_list.index(1,0)
    idx2 = sp_list.index(1,idx1+1)
    idx3 = sp_list.index(1,idx2+1)
    
    numer = len(sp_list)*[0]
    numer[idx1] = 1
    numer[idx2] = 1    
    value = CM.Variables().getMoment(tuple(numer))
    
    numer = len(sp_list)*[0]
    numer[idx1] = 1
    numer[idx3] = 1    
    value *= CM.Variables().getMoment(tuple(numer))

    numer = len(sp_list)*[0]
    numer[idx2] = 1
    numer[idx3] = 1    
    value *= CM.Variables().getMoment(tuple(numer))

    denom = len(sp_list)*[0]
    denom[idx1] = 1 
    value = value/CM.Variables().getMoment(tuple(denom))
    
    denom = len(sp_list)*[0]
    denom[idx2] = 1 
    value = value/CM.Variables().getMoment(tuple(denom))
    
    denom = len(sp_list)*[0]
    denom[idx3] = 1 
    value = value/CM.Variables().getMoment(tuple(denom))
    
    
    return value

def convert(sp_tup):
    
    method_dict = {(0,0,1,0):Method1, (1,1,0,0):Method2, (3,0,0,0):Method3}

    
    sp_list = list(sp_tup)
    no_1s = sp_list.count(1)
    no_2s = sp_list.count(2)
    no_3s = sp_list.count(3)
    dict_key = (no_1s, no_2s, no_3s, 0)

    try:

        return method_dict[dict_key](sp_tup)
    except:
        print sp_tup, dict_key
        raise 'Exit'
